// 实现这个项目的构建任务
const { src, dest, parallel, series, watch } = require('gulp')
const browserSync = require('browser-sync')
const loadPlugins = require('gulp-load-plugins')
const plugins = loadPlugins()
//配置服务器
const bs = browserSync.create() //创建服务器
// 对样式的构建
const style = () => {
  return src('src/assets/styles/*.scss', { base: 'src' }).pipe(plugins.sass({ outputStyle: 'expanded' })).pipe(dest('temp')).pipe(bs.reload({ stream: true }))
}
// 对脚本的构建
const script = () => {
  return src('src/assets/scripts/*.js', { base: 'src' }).pipe(plugins.babel({ presets: ['@babel/preset-env'] })).pipe(dest('temp')).pipe(bs.reload({ stream: true }))
}
// 对图片字体构建
const image = () => {
  return src('src/assets/images/*', { base: 'src' }).pipe(plugins.imagemin()).pipe(dest('dist')).pipe(bs.reload({ stream: true }))
}
const font = () => {
  return src('src/assets/fonts/**', { base: 'src' }).pipe(plugins.imagemin()).pipe(dest('dist')).pipe(bs.reload({ stream: true }))
}
// 对模板渲染
const data = {
  menus: [
    {
      name: '主页',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: '关于',
      link: 'about.html'
    },
    {
      name: '内容',
      link: '#',
      children: [
        {
          name: '内容1',
          link: 'index.html'
        },
        {
          name: '内容2',
          link: 'index.html'
        },
        {
          name: '内容3'
        },
        {
          name: '内容4',
          link: 'index.html'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}
const page = () => {
  return src('src/**.html', { base: 'src' }).pipe(plugins.swig({
    data, defaults: { cache: false }
  })).pipe(dest('temp')).pipe(bs.reload({ stream: true }))
}
// 加入额外文件
const extra = () => {
  return src('public/**', { base: 'public' }).pipe(dest('dist')).pipe(bs.reload({ stream: true }))
}
// 清除文件 cnpm install del --dev
const del = require('del')
const clean = () => {
  return del(['dist', 'temp'])//返回一个promise对象
}


const serve = () => {
  watch('src/assets/styles/*.scss', style),//监听文件是否改变了，
    watch('src/assets/scripts/*.js', script),
    watch('src/**.html', page),
    watch(['src/assets/images/*', 'src/assets/fonts/**', 'public/**'], bs.reload),
    bs.init({
      notify: false,//是否提示更改
      port: 3000,//配置端口
      open: true,//是否自动打开浏览器，默认true
      files: 'temp/**',//设置被监听的文件路径,让浏览器进行实时刷新，如果不设置它，就需要在每个任务中添加.pipe(bs.reload({stream:true}))
      server: {
        baseDir: ['temp', 'src', 'public'],//配置网站访问的根目录，当dist中找不到就会去src,然后又可以匹配public
        routes: {
          '/node_modules': 'node_modules' //将有些路径不能在dist上访问的，在这里做映射关系
        }
      }
    })
}
const useref = () => {
  // 这是在已经编译好后，对文件引用进行处理,和压缩
  return src('temp/*.html', { base: 'temp' }).pipe(plugins.useref({ searchPath: ['temp', '.'] }))
    .pipe(plugins.if(/\.js$/, plugins.uglify()))
    .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
    .pipe(plugins.if(/\.html$/, plugins.htmlmin({ collapseWhitespace: true, minifyCSS: true, minifyJS: true })))
    .pipe(dest('dist')) // 需要重新制定位置，因为上面一直在读，下面又在写，会导致冲突
}

const compile = parallel(style, script, page)
// 上线之前执行的任务
const build = series(clean, parallel(series(compile, useref), image, font, extra))
const develop = series(compile, serve)
module.exports = {
  clean,
  build,
  develop
}